package itsm.isperp.module.service.bpm;

import itsm.isperp.framework.service.BaseService;
import itsm.isperp.module.entity.bpm.BpmNodeUser;
import itsm.isperp.module.repository.bpm.BpmNodeUserMapper;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * 流程人员服务
 * 
 * @author lizx
 * @date 2013-6-13
 * 
 */
@Service
public class BpmNodeUserService extends BaseService<BpmNodeUser> {

	@Resource
	public BpmNodeUserMapper bpmNodeUserMapper;

	@Autowired
	org.activiti.engine.RepositoryService repositoryService;

	@Override
	public BpmNodeUserMapper getRepository() {

		return bpmNodeUserMapper;
	}

	/**
	 * 根据节点配置Id获取节点的
	 * 
	 * @param bpmNodeSettingId
	 *            节点设置的id
	 * @return
	 */
	public List<BpmNodeUser> findAllByNodeSettingId(
			final String bpmNodeSettingId) {
		return this.getRepository().findAllByNodeSettingId(bpmNodeSettingId);
	}

	public List<BpmNodeUser> findAllByDefIdAndNodeId(final String defId,
			final String nodeId) {
		return this.getRepository().findAllByDefIdAndNodeId(defId, nodeId);
	}

	/**
	 * 
	 * @param defKey
	 * @param nodeId
	 * @return
	 */
	public List<BpmNodeUser> findAllCandidaterByDefKeyAndNodeId(
			final String defKey, final String nodeId) {
		// 取得流程定义
		ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey(defKey).latestVersion().singleResult();

		return this.getRepository().findAllByDefIdAndNodeId(pd.getId(), nodeId);

	}

	/**
	 * 
	 * @param defKey
	 * @param nodeId
	 * @return
	 */
	public List<BpmNodeUser> findAllCandidaterByDefIdAndNodeId(
			final String defId, final String nodeId) {

		return this.getRepository().findAllByDefIdAndNodeId(defId, nodeId);

	}

	public Map<String, Object> findDbEntityWithDomainFieldByNodeId(String nodeId) {
		return this.getRepository().findDbEntityWithDomainFieldByNodeId(nodeId);
	}
}
